package net.starwifi.duobao.hfg.notify;

import cn.com.hongpay.client.utils.SignatureTools;
import com.alibaba.fastjson.JSONObject;
import net.starwifi.duobao.hfg.APIRequest;

import java.util.List;

/**
 * Created by STX on 2016/4/11.
 */
public class NotifyRequest extends APIRequest {

    List<NotifyTradeDetail> trade_details;

    private String partner_id;

    private String signed_msg;

    public NotifyRequest() {
    }

    public NotifyRequest(List<NotifyTradeDetail> trade_details, String partner_id, String signed_msg) {
        this.trade_details = trade_details;
        this.partner_id = partner_id;
        this.signed_msg = signed_msg;
    }

    public void parseReq(String data){
        System.out.println("通知json:"+data);
        JSONObject json = JSONObject.parseObject(data);
        JSONObject body=JSONObject.parseObject(json.get("body").toString());
        JSONObject info=JSONObject.parseObject(json.get("info").toString());

        String detailsJson=body.get("trans_details").toString();
        trade_details=JSONObject.parseArray(detailsJson,NotifyTradeDetail.class);
        partner_id=info.get("partner_id").toString();
        signed_msg=info.get("signed_msg").toString();
//        NotifyRequest notify=new NotifyRequest(trade_details,partner_id,signed_msg);
//        return notify;
    }

    @Override
    public String makeSign(String pfxFile, String pfxPwd, String privateKeyPwd) {
        NotifyTradeDetail tradeDetail=trade_details.get(0);
        StringBuffer sb=new StringBuffer();
        sb.append("partner_id:"+partner_id);
        sb.append(";trade_no:"+tradeDetail.getTrade_no());
        sb.append(";partner_trade_no:"+tradeDetail.getPartner_trade_no());
        sb.append(";payer_partner_id:"+tradeDetail.getPayer_partner_id());
        sb.append(";payer_partner_telephone:"+tradeDetail.getPayer_partner_telephone());
        sb.append(";amount:"+tradeDetail.getAmount());
        sb.append(";result_code:"+tradeDetail.getResult_code()+";");
        SignatureTools st = new SignatureTools();
        System.out.println("makeSign:"+sb.toString());
        String sign= st.sign(sb.toString(),pfxFile, pfxPwd, privateKeyPwd);
        System.out.println("加密makeSign:"+sb.toString());
        return sign;
    }

    @Override
    public String encode(String pfxFile, String pfxPwd, String privateKeyPwd) {
        signed_msg=makeSign(pfxFile,pfxPwd,privateKeyPwd);
        StringBuffer infoJson= new StringBuffer();
        infoJson.append("partner_id:"+partner_id);
        infoJson.append(",signed_msg:"+signed_msg);

        String detailJson=JSONObject.toJSONString(trade_details);
        String json="{ body:{trade_details:"+detailJson+",info:{"+infoJson.toString()+"}"+"}";
        return json;
    }

    public List<NotifyTradeDetail> getTrade_details() {
        return trade_details;
    }

    public void setTrade_details(List<NotifyTradeDetail> trade_details) {
        this.trade_details = trade_details;
    }

    public String getPartner_id() {
        return partner_id;
    }

    public void setPartner_id(String partner_id) {
        this.partner_id = partner_id;
    }

    public String getSigned_msg() {
        return signed_msg;
    }

    public void setSigned_msg(String signed_msg) {
        this.signed_msg = signed_msg;
    }
}
